/*
 * Copyright 2025-2026 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @author brianxiadong
 */
package org.springframework.ai.mcp.sample.server;

import org.springframework.ai.tool.annotation.Tool;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 利用OpenMeteo的免费天气API提供天气服务
 * 该API无需API密钥，可以直接使用
 */
@Service
public class QueryNameService {

    //生成一些实例的用户信息
    private static final List<UserInfo> list = List.of(
            new UserInfo(1, "John", "2023-07-15", 60.0),
            new UserInfo(2, "Alice", "2023-07-16", 70.0),
            new UserInfo(3, "Bob", "2023-07-17", 80.0)
    );


    /**
     * 根据用户id查询用户的名称
     *
     * @param tenantId 租户id
     * @param userId   用户id
     * @return 返回用户的名称
     */
    @Tool(description = "根据用户id查询用户的名称")
    public String queryName(String tenantId,Integer userId) {
        if(!tenantId.equals("yawen")){
           throw new RuntimeException("租户id不正确");
        }
        for (UserInfo userInfo : list) {
            if (userInfo.getId() == userId) {
                return userInfo.getName();
            }
        }
        return "未找到相关信息";
    }

    /**
     * 根据用户id查询用户的出生日期
     *
     * @param tenantId 租户id
     * @param userId   用户id
     * @return
     */
    @Tool(description = "根据用户id查询用户的出生日期")
    public String queryDate(String tenantId,Integer userId) {
        if(!tenantId.equals("yawen")){
            throw new RuntimeException("租户id不正确");
        }
        for (UserInfo userInfo : list) {
            if (userInfo.getId() == userId) {
                return userInfo.getDate();
            }
        }
        return "未找到相关信息";
    }

    /**
     * 根据用户id查询用户的相关信息
     *
     * @param tenantId 租户id
     * @param userId   用户id
     * @return
     */
    @Tool(description = "根据用户id查询用户的相关信息")
    public UserInfo queryUserInfo(String tenantId,Integer userId) {
        if(!tenantId.equals("yawen")){
            throw new RuntimeException("租户id不正确");
        }
        for (UserInfo userInfo : list) {
            if (userInfo.getId() == userId) {
                return userInfo;
            }
        }
        return null;
    }

}